package com.love.marriage.dictInfo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.love.marriage.common.interfaces.avoid.AvoidRepeatableCommit;
import com.love.marriage.common.interfaces.avoid.LogRestRequest;
import com.love.marriage.common.validator.Insert;
import com.love.marriage.common.validator.Update;
import com.love.marriage.common.vo.Result;
import com.love.marriage.common.vo.ResultBoolean;
import com.love.marriage.dictInfo.service.DictInfoService;
import com.love.marriage.dictInfo.vo.DictInfoVo;
import com.love.marriage.dictInfo.vo.ResultDictInfoVo;
import com.love.marriage.dictInfo.vo.ResultDictInfoVoList;
import com.love.marriage.dictInfo.vo.ResultDictInfoVoPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * description:  字典信息表restApi
 * author:       mybatisPlus
 * createDate:   2024-10-12 22:54:04
 * version:      1.0.0
 */
@ApiSort(105)
@Tag(name = "字典信息表相关接口", description = "字典信息表相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/dict-info")
public class DictInfoController {

    private final DictInfoService dictInfoService;

    @ApiOperationSupport(order = 10, author = "alex")
    @Operation(summary = "获取字典信息表分页", description = "获取字典信息表分页")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultDictInfoVoPage.class
                    )))
    @PostMapping(value = "/page")
    @Parameters({
            @Parameter(name = "pageNum", description = "页码", required = false),
            @Parameter(name = "pageSize", description = "每页大小", required = false)
    })
    public Result<Page<DictInfoVo>> getPage(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                            @RequestParam(value = "pageSize", required = false) Long pageSize,
                                            @RequestBody(required = false) DictInfoVo dictInfoVo) {
        return Result.success(dictInfoService.getPage(pageNum, pageSize, dictInfoVo));
    }

    @ApiOperationSupport(order = 20, author = "alex")
    @Operation(summary = "获取字典信息表详情", description = "获取字典信息表详情")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultDictInfoVo.class
                    )))
    @Parameters({
            @Parameter(name = "id", description = "id", required = true, schema = @Schema(implementation = Long.class, type = "Long", example = "1"))
    })
    @GetMapping
    public Result<DictInfoVo> query(@RequestParam(value = "id") Long id) {
        return Result.success(dictInfoService.queryDictInfo(id));
    }

    @ApiOperationSupport(order = 25, author = "alex")
    @LogRestRequest(apiName = "用户密码登录")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultDictInfoVo.class
                    )))
    @Parameters({
            @Parameter(name = "typeCode", description = "编码(showMember)", required = true, schema = @Schema(implementation = String.class, type = "String", example = "1")),
            @Parameter(name = "belongTo", description = "类型(showInfo)", required = true, schema = @Schema(implementation = String.class, type = "String", example = "1"))
    })
    @GetMapping(value = "/queryConfig")
    @Tag(name = "公共", description = "公共接口")
    public Result<DictInfoVo> queryConfig(@RequestParam(value = "typeCode") String typeCode,
                                          @RequestParam(value = "belongTo") String belongTo) {
        return Result.success(dictInfoService.queryDictInfoByTypeCode(typeCode,belongTo));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 30, author = "alex")
    @Operation(summary = "新增字典信息表", description = "新增字典信息表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = DictInfoVo.class))
            ))
    @LogRestRequest(apiName = "新增字典信息表")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultDictInfoVo.class)))
    @PostMapping
    public Result<DictInfoVo> add(@Validated({Insert.class}) @RequestBody DictInfoVo dictInfoVo) {
        return Result.success(dictInfoService.addDictInfo(dictInfoVo));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 25, author = "alex")
    @Operation(summary = "批量新增字典信息表", description = "批量新增字典信息表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(type = "array", implementation = DictInfoVo.class))
            ))
    @LogRestRequest(apiName = "批量新增字典信息表")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultBoolean.class)))
    @PostMapping(value = "batchAddDictInfo")
    public Result<Boolean> addBatch(@Validated({Insert.class}) @RequestBody List<DictInfoVo> dictInfoVo) {
        return Result.success(dictInfoService.batchAddDictInfo(dictInfoVo));
    }

    @ApiOperationSupport(order = 40, author = "alex")
    @Operation(summary = "修改字典信息表", description = "修改字典信息表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = DictInfoVo.class))
            ))
    @LogRestRequest(apiName = "修改字典信息表")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultDictInfoVo.class)))
    @PutMapping
    public Result<DictInfoVo> update(@Validated({Update.class}) @RequestBody DictInfoVo dictInfoVo) {
        return Result.success(dictInfoService.updateDictInfo(dictInfoVo));
    }

    @ApiOperationSupport(order = 50, author = "alex")
    @Operation(summary = "刪除字典信息表", description = "刪除字典信息表")
    @LogRestRequest(apiName = "刪除字典信息表")
    @Parameters({
            @Parameter(name = "ids", description = "ids,用，分割", required = true, schema = @Schema(implementation = String.class, type = "String", example = "1,2"))
    })
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultBoolean.class)))
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam("ids") String ids) {
        return Result.success(dictInfoService.deleteDictInfo(ids));
    }

    @ApiOperationSupport(order = 60, author = "alex")
    @Operation(summary = "获取字典列表", description = "获取字典列表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = DictInfoVo.class))
            ))
    @LogRestRequest(apiName = "获取字典列表")
    @GetMapping(value = "/listByBelong")
    @Parameters({
            @Parameter(name = "belongTo", description = "分类信息，用，分割")}
    )
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultDictInfoVoList.class)))
    @Tag(name = "公共", description = "公共接口")
    public Result<List<DictInfoVo>> listByBelong(@RequestParam(value = "belongTo", required = false) String belongTo) {
        return Result.success(dictInfoService.listByBelong(belongTo));
    }

    @ApiOperationSupport(order = 70, author = "alex")
    @Operation(summary = "初始化字典数据", description = "初始化字典数据")
    @LogRestRequest(apiName = "初始化字典数据")
    @GetMapping(value = "/initDict")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultBoolean.class)))
    public Result<Boolean> initDictRedis() {
        return Result.success(dictInfoService.initDictRedis());
    }
}
